//
//  smContractSearchController.m
//  shimaoHouse
//
//  Created by haifan wu on 12-6-28.
//  Copyright (c) 2012年 yst. All rights reserved.
//

#import "smContractSearchController.h"
#import "smContratDetailController.h"
#import "SUPApplication.h"
#import "smMboSmMboDB.h"
#import "SUPConnectionProperties.h"
#import "SUPLoginCredentials.h"
#import "smMboSmTbContract.h"
#import "SUPConnectionProfile.h"
#import "HTTPImageView.h"


@interface smContractSearchController ()<UITableViewDelegate, UITableViewDataSource,UITextFieldDelegate,UISearchBarDelegate>
@property (nonatomic, strong) UITableView* myTableView;
@property (nonatomic, strong) UISearchBar* mySearBar;
@end


@implementation smContractSearchController
@synthesize myTableView = _myTableView;
@synthesize mySearBar = _mySearBar;
@synthesize contractList = _contractList;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
      
	// Do any additional setup after loading the view.
    //[self setTitle:@"合同列表"];
    [self.navigationController.navigationBar setHidden:YES];
    UIImageView* innerImageView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"contactlist_title.png"]];
    innerImageView.frame=CGRectMake(0, 0, 770, 70);
    innerImageView.contentMode = UIViewContentModeScaleToFill;
    //self.navigationItem.titleView =innerImageView;
    [self.view addSubview:innerImageView]; 

    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 68, self.view.bounds.size.width, 
                                                                     self.view.bounds.size.height-150) //style:UITableViewStyleGrouped
                        ];  
    //self.ystTableView = [[UITableView alloc] initWithFrame:CGRectMake()];
    [self.myTableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
    [self.myTableView setDelegate:self];
    [self.myTableView setDataSource:self];
    
    self.mySearBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.bounds.size.width, 80)];  
    self.mySearBar.placeholder=@"输入关键字";  
    self.mySearBar.delegate = self;  
    self.myTableView.tableHeaderView = self.mySearBar;  
    self.mySearBar.autocorrectionType = UITextAutocorrectionTypeNo;  
    self.mySearBar.autocapitalizationType = UITextAutocapitalizationTypeNone;  
    
    UIImage* searchbarImg = [UIImage imageNamed:@"search.png"];
    [self.mySearBar setBackgroundImage:searchbarImg];
    
    UIButton* backBt = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBt setFrame: self.view.bounds];
    [backBt setBackgroundColor:[UIColor clearColor]];
    //定义点击时的响应函数
	[backBt addTarget:self action:@selector(missKeyBoard) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:backBt];
    
    [self.view addSubview: self.myTableView];
    
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
-(void) buttonClick:(UIButton*) bt {
    NSLog(@"buttonClick");
    //    
    
    //[self.mytextFiled resignFirstResponder];
    //    
    //    UINavigationController* tem = self.navigationController;
    
    //    self.navationCt = [[UINavigationController alloc] initWithRootViewController:self];
    
    //    testControll01* controller = [[testControll01 alloc] init];
    
    //    [[self parentViewController].navigationController pushViewController:controller animated:YES];
    //    [self.navigationController pushViewController:controller animated:YES];
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
    
    //[self.myLabel setText:textField.text];
}// may be called if forced even if

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}// Default is 1 if not implemented


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.contractList count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSString* CellIdentifier = [NSString stringWithFormat:@"%d%d",indexPath.section, indexPath.row];
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleGray;
    } 
    
    for (UIView *v in [cell subviews]) {
        if ([v isKindOfClass:[UILabel class]] | [v isKindOfClass:[UIImageView class]] | [v isKindOfClass:[UISearchBar class]]) {
            
            [v removeFromSuperview];
        }
    }    
        //右边加箭头
        //cell.accessoryType =  UITableViewCellAccessoryDisclosureIndicator;
        NSUInteger row = [indexPath row];
        HTTPImageView* imgView = [[HTTPImageView alloc] init];     
       [imgView setUrl:[NSString stringWithFormat:@"http://10.91.2.14:8080/upload_file/mobile/house%d.png",row+1]];
      
        [imgView setFrame:CGRectMake(2, 4, 200, 160)];
        [cell addSubview:imgView];
//    
	    
        smMboSmTbContract *contract = [self.contractList objectAtIndex:row];
        
        UILabel* tempLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(220, 10, 200, 30)];
        [tempLabel1 setBackgroundColor:[UIColor clearColor]];
        tempLabel1.font = [UIFont systemFontOfSize:29];
        tempLabel1.textColor=[UIColor colorWithRed:11/255.0 green:122/255.0 blue:182/255.0 alpha:1.0];
        [tempLabel1 setText:[NSString stringWithFormat:@"合同编号：%@",[contract id_]]];
        [cell addSubview:tempLabel1];
    
//    UILabel* tempLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(220, 10, 400, 30)];
//    [tempLabel1 setBackgroundColor:[UIColor clearColor]];
//    tempLabel1.font = [UIFont systemFontOfSize:29];
//    tempLabel1.textColor=[UIColor colorWithRed:11/255.0 green:122/255.0 blue:182/255.0 alpha:1.0];
//    [tempLabel1 setText:[NSString stringWithFormat:@"合同编号：A000-1100"]];
//    [cell addSubview:tempLabel1];
        
        
        UILabel* tempLabel3 = [[UILabel alloc] initWithFrame:CGRectMake(220, 50, 200, 30)];
        [tempLabel3 setBackgroundColor:[UIColor clearColor]];
        tempLabel3.font = [UIFont systemFontOfSize:22];
        tempLabel3.textColor=[UIColor colorWithRed:122/255.0 green:122/255.0 blue:122/255.0 alpha:1.0];
        NSString * state=@"";
        if([[contract isReceivables] isEqualToString:@"1"] ){
            state = @"部分收款";
        }else if([[contract isReceivables]isEqualToString:@"2"]){
            state = @"全部收款";
        }else{
            state = @"未收款";
        }
        [tempLabel3 setText:[NSString stringWithFormat:@"当前状态：%@",state]];
        [cell addSubview:tempLabel3];
    
//    UILabel* tempLabel3 = [[UILabel alloc] initWithFrame:CGRectMake(220, 50, 400, 30)];
//    [tempLabel3 setBackgroundColor:[UIColor clearColor]];
//    tempLabel3.font = [UIFont systemFontOfSize:22];
//    tempLabel3.textColor=[UIColor colorWithRed:122/255.0 green:122/255.0 blue:122/255.0 alpha:1.0];
//    [tempLabel3 setText:[NSString stringWithFormat:@"当前状态：已完成"]];
//    [cell addSubview:tempLabel3];
        

        UILabel* tempLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(220, 80, 200, 30)];
        [tempLabel2 setBackgroundColor:[UIColor clearColor]];
         tempLabel2.font = [UIFont systemFontOfSize:22];
         tempLabel2.textColor=[UIColor colorWithRed:122/255.0 green:122/255.0 blue:122/255.0 alpha:1.0];
        [tempLabel2 setText:[NSString stringWithFormat:@"客户名称：%@",[contract firstParty]]];
        [cell addSubview:tempLabel2];
    
//    UILabel* tempLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(220, 80, 400, 30)];
//    [tempLabel2 setBackgroundColor:[UIColor clearColor]];
//    tempLabel2.font = [UIFont systemFontOfSize:22];
//    tempLabel2.textColor=[UIColor colorWithRed:122/255.0 green:122/255.0 blue:122/255.0 alpha:1.0];
//    [tempLabel2 setText:[NSString stringWithFormat:@"客户名称：张生"]];
//    [cell addSubview:tempLabel2];

    //cell.selectionStyle = UITableViewCellSelectionStyleBlue;
    UIImage* seletedImg = [UIImage imageNamed:@"tableview_seletedBakground.png"];    
    UIImageView *seletedImgView =[[UIImageView alloc] initWithImage:seletedImg];
    [cell setSelectedBackgroundView:seletedImgView];
    
    
    UIImage* backImg = [UIImage imageNamed:@"tableview_background.png"];    
    UIImageView *backImgView =[[UIImageView alloc] initWithImage:backImg];
    [cell setBackgroundView:backImgView];
    
    return cell;
    
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 171;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0;    
}


- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0;
}


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return @"";
}// fixed font style. use custom view (UILabel) if you want something different
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
    return @"";
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSUInteger row = [indexPath row];
    smMboSmTbContract *contract = [self.contractList objectAtIndex:row];
    smContratDetailController *contratDetailController=[[smContratDetailController alloc] init];
    [contratDetailController setItems:contract title:@"dddd"];
    [self.navigationController pushViewController:contratDetailController animated:YES];
    
}
//搜索框查询事件
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    NSMutableArray *array = [[NSMutableArray alloc] init];
    SUPQuery *myquery = [SUPQuery getInstance];
    myquery.testCriteria = [SUPAttributeTest
                            match:@"firstParty" :searchBar.text];
    SUPObjectList* list=[smMboSmTbContract findWithQuery:myquery];
    
    NSString* temp = [NSString stringWithFormat:@"list size is %d",[list size]];
    
    NSLog(temp,nil);
    
    if ([list length] > 0)
    {
        for (smMboSmTbContract * oneRec in list)
        {
            [array addObject:oneRec];
        }
    }
    self.contractList = array;
    
    [self.myTableView reloadData]; 
    //取消键盘
}

-(void) missKeyBoard {
    
    [self.mySearBar resignFirstResponder]; 
}

- (void)viewWillAppear:(BOOL)animated {
    NSMutableArray *array = [[NSMutableArray alloc] init];
    [smMboSmMboDB beginSynchronize];
    SUPObjectList* list=[smMboSmTbContract findAll];
    
    NSString* temp = [NSString stringWithFormat:@"list size is %d",[list size]];
    
    NSLog(temp,nil);
    
    if ([list length] > 0)
    {
        for (smMboSmTbContract * oneRec in list)
        {
            [array addObject:oneRec];
        }
    }
    self.contractList = array;
    
    [self.myTableView reloadData]; 
    [super viewWillAppear:YES];
    
}

@end
